/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sjtu.servlet;

import com.sjtu.bean.BookBean;
import com.sjtu.bean.CategoryBean;
import com.sjtu.mail.MailTest;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author p w
 */
public class AddBookServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doWork(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doWork(request, response);
    }

    public void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String obj = request.getParameter("obj");
//        System.out.println(obj);
        JSONObject jsonObj = null;
        try {
            jsonObj = new JSONObject(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PrintWriter writer = response.getWriter();
        try {
            String bookname = jsonObj.getString("bookname");
//            System.out.println(bookname + "\n");
            String author = jsonObj.getString("author");
            String description = jsonObj.getString("description");
            String coverURL = jsonObj.getString("coverURL");
            String category = jsonObj.getString("category");
            int categoryId = CategoryBean.getCategory(category).getId();
            ArrayList<String> categoryUserMailList = CategoryBean.categoryUsers(categoryId);
            MailTest mail = new MailTest();
            Session session = mail.initMes();
            for (int i = 0; i < categoryUserMailList.size(); i++) {
                mail.sendMes(categoryUserMailList.get(i), session,
                        "we've got a new book in our library you may be interested in.\n bookName:" 
                        + bookname + "\n author：" + author + "\n " + description + "\n" + coverURL, "new book :" + bookname);
            }
            //       String parentCategory = jsonObj.getString("parentCategory");
            String isbn = jsonObj.getString("isbn");
            String publisher = jsonObj.getString("publisher");
            String bookScoreString = jsonObj.getString("bookScore");
            int bookScore = Integer.parseInt(bookScoreString);
//            String label1 = jsonObj.getString("label1"); 
//            String label2 = 
            String libraryName = jsonObj.getString("library");
            String libraryAddress = libraryName;
            String bookNumberString = jsonObj.getString("number");
            int bookNumber = Integer.parseInt(bookNumberString);
            Date dateNow = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String addDate = dateFormat.format(dateNow);
//            BookBean book = BookBean.addBook("test", "test", "test", "http://test.com", "testchild", "test", "test", "test", 1);
            BookBean book = BookBean.addBook(bookname, author, description, coverURL, category, publisher, isbn, bookScore);
            book.setLibrary(libraryName, libraryAddress, bookNumber);
            book.setLabel(jsonObj.getString("label1"));
            book.setLabel(jsonObj.getString("label2"));
            book.setLabel(jsonObj.getString("label3"));
            book.setLabel(jsonObj.getString("label4"));
            book.setLabel(jsonObj.getString("label5"));
            book.setLabel(jsonObj.getString("label6"));
            ArrayList<String> labelArray = book.getLabel();
            JSONObject jsonRes = new JSONObject();
            jsonRes.put("id", book.getId());
            jsonRes.put("bookname", book.getName());
            String showlabel = "";
            for (int i = 0; i < labelArray.size(); i++) {
                showlabel += labelArray.get(i);
            }
            jsonRes.put("label", showlabel);
            writer.write(jsonRes.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        writer.flush();
        writer.close();
    }
}
